/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Lop;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;

/**
 *
 * @author Administrator
 */
public class lNHOM_NEN {
    private String Manhom;
    private String Tennhom;
    private ArrayList<lGia> Banggia;
    private lKHU_TDC refKhutdc;
    
    public lNHOM_NEN(){}
    public lNHOM_NEN(String manhom, String tennhom){
        this.Manhom = manhom;
        this.Tennhom = tennhom;
    }
    public lNHOM_NEN(String manhom, String tennhom, lKHU_TDC lkhutdc){
        this.Manhom = manhom;
        this.Tennhom = tennhom;
        this.refKhutdc = lkhutdc;
    }
    
    public String getManhom() {
        return Manhom;
    }

    public void setManhom(String Manhom) {
        this.Manhom = Manhom;
    }

    public String getTennhom() {
        return Tennhom;
    }

    public void setTennhom(String Tennhom) {
        this.Tennhom = Tennhom;
    }

    public ArrayList<lGia> getBanggia() {
        return Banggia;
    }

    public void setBanggia(ArrayList<lGia> Banggia) {
        this.Banggia = Banggia;
    }

    public lKHU_TDC getRefKhutdc() {
        return refKhutdc;
    }

    public void setRefKhutdc(lKHU_TDC refKhutdc) {
        this.refKhutdc = refKhutdc;
    }
    
    public boolean insert(OConnection cnn) throws SQLException, ClassNotFoundException{
        String queryStr = "Insert into NHOM_NEN values(T_NHOM_NEN(?,?,TAB_GIA()," +
                                "(Select ref(p) from KHU_TDC p where p.makhu = ?)))";
        OraclePreparedStatement pst;
        Connection conn = cnn.createConnection();
        pst = (OraclePreparedStatement)conn.prepareStatement(queryStr);
        pst.setString(1, Manhom);
        pst.setString(2, Tennhom);
        pst.setString(3, refKhutdc.getMakhu());
        pst.executeQuery();
        conn.close();
        return true;
    }
    
    public boolean update(OConnection cnn) throws SQLException, ClassNotFoundException{
        String queryStr = "Update NHOM_NEN set Tennhom=? where Manhom = ?";
        OraclePreparedStatement pst;
        Connection conn = cnn.createConnection();
        pst = (OraclePreparedStatement)conn.prepareStatement(queryStr);
        pst.setString(1, Tennhom);
        pst.setString(2, Manhom);
        pst.executeQuery();
        conn.close();
        return true;
    }
    
    public boolean delete(OConnection cnn) throws SQLException, ClassNotFoundException{
        String queryStr = "Delete from NHOM_NEN where Manhom = ?";
        OraclePreparedStatement pst;
        Connection conn = cnn.createConnection();
        pst = (OraclePreparedStatement)conn.prepareStatement(queryStr);
        pst.setString(1, Manhom);
        pst.executeQuery();
        conn.close();
        return true;
    }
    
    public boolean insertGia(OConnection cnn) throws SQLException, ClassNotFoundException{
        String queryStr = "Insert into the (select banggia from NHOM_NEN where manhom=?) values(T_GIA(?,?))";
        OraclePreparedStatement pst;
        Connection conn = cnn.createConnection();
        pst = (OraclePreparedStatement)conn.prepareStatement(queryStr);
        pst.setString(1, Manhom);
        java.sql.Date mydate = new java.sql.Date(Banggia.get(0).getNgay().getTime());
        pst.setDate(2, mydate);
        pst.setInt(3, Banggia.get(0).getGia());
        pst.executeQuery();
        conn.close();
        return true;
    }
    
    public boolean updateGia(OConnection cnn) throws SQLException, ClassNotFoundException{
        String queryStr = "Update the (select banggia from NHOM_NEN where manhom=?) set gia = ? where ngay = ?";
        OraclePreparedStatement pst;
        Connection conn = cnn.createConnection();
        pst = (OraclePreparedStatement)conn.prepareStatement(queryStr);
        pst.setString(1, Manhom);
        pst.setInt(2, Banggia.get(0).getGia());
        java.sql.Date mydate = new java.sql.Date(Banggia.get(0).getNgay().getTime());
        pst.setDate(3, mydate);
        pst.executeQuery();
        conn.close();
        return true;
    }
    
    public boolean deleteGia(OConnection cnn) throws SQLException, ClassNotFoundException{
        String queryStr = "Delete from the (select banggia from NHOM_NEN where Manhom = ?) where ngay = ?";
        OraclePreparedStatement pst;
        Connection conn = cnn.createConnection();
        pst = (OraclePreparedStatement)conn.prepareStatement(queryStr);
        pst.setString(1, Manhom);
        java.sql.Date mydate = new java.sql.Date(Banggia.get(0).getNgay().getTime());
        pst.setDate(2, mydate);
        pst.executeQuery();
        conn.close();
        return true;
    }
    
    public OracleResultSet getBanggia(OConnection cnn) throws SQLException, ClassNotFoundException{
        String queryStr = "select * from the (select banggia from NHOM_NEN p where p.manhom = ?)";
        OraclePreparedStatement pst;
        OracleResultSet rs;
        Connection conn = cnn.createConnection();
        pst = (OraclePreparedStatement)conn.prepareStatement(queryStr);
        pst.setString(1, Manhom);
        rs = (OracleResultSet)pst.executeQuery();
        //conn.close();
        return rs;
    }
    
}
